package com.lym.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lym.reggie.common.CustomException;
import com.lym.reggie.entity.Setmeal;
import com.lym.reggie.entity.SetmealDish;
import com.lym.reggie.entity.SetmealDto;
import com.lym.reggie.mapper.SetmealMapper;
import com.lym.reggie.service.SetmealDishService;
import com.lym.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    //新增套餐，同时需要保存套餐和菜品的关联关系
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //套餐保存到setmeal表、套餐和菜品对应保存到setmealDish表
        // 1 保存套餐的基本信息，操作setmeal,执行insert
        this.save(setmealDto);
        // 2.保存套餐和菜品的关联信息，操作setmeal_dish ,执行insert操作
        // 遍历保存进表 同时设置setmealId
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(setmealDish -> {
            //注意上面拿到的setmealDishes是没有setmeanlId这个的值的，通过debug可以发现
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;//这里返回的就是集合的泛型
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);//批量保存
    }

    //删除套餐 逻辑删除
    @Transactional
    public void deleteByIdWithDish(List<Long> ids){
        // 1 查看是否满足删除条件 是否在启售
        // 2 先删除SetmealDish
        // 3 删除setmeal

        //sql语句应该是这样的:select count(*) setmeal where id in () and status = 1;
        //查询套餐的状态，看是否可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(queryWrapper);
        //如果不能删除，抛出一个业务异常
        if (count > 0){
            throw new CustomException("套餐正在售卖中,不能删除");
        }
        //如果可以删除，先删除套餐表中的数据--setmeal
        this.removeByIds(ids);
        //删除关系表中的数据--setmeal_dish
        //delete from setmeal_dish where setmeal_id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(lambdaQueryWrapper);

    }

    @Override
    public SetmealDto getDate(long id) {
        // 根据id查询Setmeal详细信息
        Setmeal setmeal = this.getById(id);
        //查询SetmealDish中对应数据
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);

        SetmealDto setmealDto = new SetmealDto();
        if (setmeal!=null){
            BeanUtils.copyProperties(setmeal,setmealDto);

            List<SetmealDish> setmealDishList = setmealDishService.list(queryWrapper);
            setmealDto.setSetmealDishes(setmealDishList);
            log.info("setmealDishList: {}",setmealDishList);
            return setmealDto;
        }
        return null;

    }

    //修改套餐
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        // 修改Setmeal
        this.updateById(setmealDto);
        // 修改SetmealDish 先删除，再添加
        Long setmealId = setmealDto.getId();

        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
            return setmealDish;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);

    }


}
